﻿#region Microsoft Public License
/*
 * (c) Copyright Zhun Qu. http://utablesdk.codeplex.com
 * 
 * This source is subject to the Microsoft Public License (Ms-PL).
 * Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
 * All other rights reserved.
 */
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Media;
using System.Windows;

namespace UTable.Objects.Shapes
{
    public class UEllipse : UShape
    {
        #region Private Members

        EllipseGeometry ellipse;
        RotateTransform rotateTransform;
        
        #endregion

        #region Constructors

        public UEllipse()
        {
            ellipse = new EllipseGeometry();
            rotateTransform = new RotateTransform();
            ellipse.Transform = rotateTransform;
            this.Stroke = Brushes.Gray;
            this.StrokeThickness = 3;
        }

        #endregion

        #region Dependency Properties

        public static readonly DependencyProperty CenterProperty = EllipseGeometry.CenterProperty.AddOwner(typeof(UEllipse), new FrameworkPropertyMetadata(new Point(0, 0), EllipsePropertyChanged));
        public Point Center
        {
            get { return (Point)GetValue(CenterProperty); }
            set { SetValue(CenterProperty, value); }
        }

        public static readonly DependencyProperty RadiusXProperty = EllipseGeometry.RadiusXProperty.AddOwner(typeof(UEllipse), new FrameworkPropertyMetadata(0.0, EllipsePropertyChanged));
        public double RadiusX
        {
            get { return (double)GetValue(RadiusXProperty); }
            set { SetValue(RadiusXProperty, value); }
        }

        public static readonly DependencyProperty RadiusYProperty = EllipseGeometry.RadiusYProperty.AddOwner(typeof(UEllipse), new FrameworkPropertyMetadata(0.0, EllipsePropertyChanged));
        public double RadiusY
        {
            get { return (double)GetValue(RadiusYProperty); }
            set { SetValue(RadiusYProperty, value); }
        }

        public static readonly DependencyProperty RotationAngleProperty = ArcSegment.RotationAngleProperty.AddOwner(typeof(UEllipse), new FrameworkPropertyMetadata(0.0, TransformPropertyChanged));
        public double RotationAngle
        {
            get { return (double)GetValue(RotationAngleProperty); }
            set { SetValue(RotationAngleProperty, value); }
        }

        static void EllipsePropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
        {
            (obj as UEllipse).EllipsePropertyChanged(args);
        }

        static void TransformPropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
        {
            (obj as UEllipse).TransformPropertyChanged(args);
        }

        void EllipsePropertyChanged(DependencyPropertyChangedEventArgs args)
        {
            ellipse.Center = Center;
            ellipse.RadiusX = RadiusX;
            ellipse.RadiusY = RadiusY;
            InvalidateMeasure();
        }

        void TransformPropertyChanged(DependencyPropertyChangedEventArgs args)
        {
            rotateTransform.Angle = RotationAngle;
            rotateTransform.CenterX = Center.X;
            rotateTransform.CenterY = Center.Y;
            InvalidateMeasure();
        }

        #endregion

        #region Override Methods

        protected override Geometry DefiningGeometry
        {
            get
            {
                return ellipse;
            }
        }

        #endregion
    }
}
